package bb.lanxing.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;

public class CusPtrClassicFrameLayout extends PtrClassicFrameLayout {
    private GestureDetector detector;
    private GestureDetector.OnGestureListener gestureListener;
    private boolean isFirst;
    private boolean mIsDisallowIntercept;
    private boolean mIsHorizontalMode;

    public CusPtrClassicFrameLayout(Context context) {
        super(context);
        this.mIsDisallowIntercept = false;
        this.mIsHorizontalMode = false;
        this.isFirst = true;
        this.gestureListener = new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {
            }

            @Override
            public void onShowPress(MotionEvent e) {
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                if (distanceX < 0.0f) {
                    distanceX = -distanceX;
                }
                if (distanceY < 0.0f) {
                    distanceY = -distanceY;
                }
                if (distanceX > distanceY) {
                    if (CusPtrClassicFrameLayout.this.isFirst) {
                        CusPtrClassicFrameLayout.this.mIsHorizontalMode = true;
                        CusPtrClassicFrameLayout.this.isFirst = false;
                    }
                    return true;
                }
                if (CusPtrClassicFrameLayout.this.isFirst) {
                    CusPtrClassicFrameLayout.this.mIsHorizontalMode = false;
                    CusPtrClassicFrameLayout.this.isFirst = false;
                }
                return false;
            }
        };
        initGesture();
    }

    public CusPtrClassicFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mIsDisallowIntercept = false;
        this.mIsHorizontalMode = false;
        this.isFirst = true;
        this.gestureListener = new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {
            }

            @Override
            public void onShowPress(MotionEvent e) {
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                if (distanceX < 0.0f) {
                    distanceX = -distanceX;
                }
                if (distanceY < 0.0f) {
                    distanceY = -distanceY;
                }
                if (distanceX > distanceY) {
                    if (CusPtrClassicFrameLayout.this.isFirst) {
                        CusPtrClassicFrameLayout.this.mIsHorizontalMode = true;
                        CusPtrClassicFrameLayout.this.isFirst = false;
                    }
                    return true;
                }
                if (CusPtrClassicFrameLayout.this.isFirst) {
                    CusPtrClassicFrameLayout.this.mIsHorizontalMode = false;
                    CusPtrClassicFrameLayout.this.isFirst = false;
                }
                return false;
            }
        };
        initGesture();
    }

    public CusPtrClassicFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mIsDisallowIntercept = false;
        this.mIsHorizontalMode = false;
        this.isFirst = true;
        this.gestureListener = new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {
            }

            @Override
            public void onShowPress(MotionEvent e) {
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                if (distanceX < 0.0f) {
                    distanceX = -distanceX;
                }
                if (distanceY < 0.0f) {
                    distanceY = -distanceY;
                }
                if (distanceX > distanceY) {
                    if (CusPtrClassicFrameLayout.this.isFirst) {
                        CusPtrClassicFrameLayout.this.mIsHorizontalMode = true;
                        CusPtrClassicFrameLayout.this.isFirst = false;
                    }
                    return true;
                }
                if (CusPtrClassicFrameLayout.this.isFirst) {
                    CusPtrClassicFrameLayout.this.mIsHorizontalMode = false;
                    CusPtrClassicFrameLayout.this.isFirst = false;
                }
                return false;
            }
        };
        initGesture();
    }

    private void initGesture() {
        this.detector = new GestureDetector(getContext(), this.gestureListener);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent e) {
        if (e.getAction() == 1) {
            this.isFirst = true;
            this.mIsHorizontalMode = false;
            this.mIsDisallowIntercept = false;
            return super.dispatchTouchEvent(e);
        } else if (this.detector.onTouchEvent(e) && this.mIsDisallowIntercept && this.mIsHorizontalMode) {
            return dispatchTouchEventSupper(e);
        } else {
            if (this.mIsHorizontalMode) {
                return dispatchTouchEventSupper(e);
            }
            return super.dispatchTouchEvent(e);
        }
    }

    @Override
    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        this.mIsDisallowIntercept = disallowIntercept;
        super.requestDisallowInterceptTouchEvent(disallowIntercept);
    }
}
